from typing import List, Optional, Dict, Any
from datetime import datetime
from decimal import Decimal
from pydantic import BaseModel, Field
from enum import Enum


class NovelStatusEnum(str, Enum):
    ongoing = "ongoing"
    completed = "completed"
    paused = "paused"


class RoleTypeEnum(str, Enum):
    protagonist = "protagonist"
    supporting = "supporting"
    antagonist = "antagonist"
    minor = "minor"


class NovelBase(BaseModel):
    title: str = Field(..., description="小说标题")
    author: str = Field(..., description="作者")
    description: Optional[str] = Field(None, description="小说简介")
    cover_image: Optional[str] = Field(None, description="封面图片URL")
    genre: Optional[str] = Field(None, description="类型")
    tags: Optional[List[str]] = Field(None, description="标签")
    status: NovelStatusEnum = Field(NovelStatusEnum.ongoing, description="状态")
    chapters: int = Field(0, description="章节数")
    word_count: int = Field(0, description="字数")
    rating: Decimal = Field(0.0, description="评分")
    view_count: int = Field(0, description="阅读数")
    discussion_count: int = Field(0, description="讨论数")


class NovelCreate(NovelBase):
    pass


class NovelUpdate(BaseModel):
    title: Optional[str] = Field(None, description="小说标题")
    author: Optional[str] = Field(None, description="作者")
    description: Optional[str] = Field(None, description="小说简介")
    cover_image: Optional[str] = Field(None, description="封面图片URL")
    genre: Optional[str] = Field(None, description="类型")
    tags: Optional[List[str]] = Field(None, description="标签")
    status: Optional[NovelStatusEnum] = Field(None, description="状态")
    chapters: Optional[int] = Field(None, description="章节数")
    word_count: Optional[int] = Field(None, description="字数")
    rating: Optional[Decimal] = Field(None, description="评分")
    view_count: Optional[int] = Field(None, description="阅读数")
    discussion_count: Optional[int] = Field(None, description="讨论数")


class NovelCharacterBase(BaseModel):
    name: str = Field(..., description="角色名称")
    description: Optional[str] = Field(None, description="角色描述")
    personality: Optional[str] = Field(None, description="性格特征")
    background: Optional[str] = Field(None, description="背景故事")
    abilities: Optional[List[str]] = Field(None, description="能力技能")
    relationships: Optional[Dict[str, Any]] = Field(None, description="人物关系")
    avatar_image: Optional[str] = Field(None, description="角色头像")
    role_type: RoleTypeEnum = Field(
        RoleTypeEnum.supporting, 
        description="角色类型"
    )
    importance_level: int = Field(5, description="重要程度(1-10)")
    tags: Optional[List[str]] = Field(None, description="标签")


class NovelCharacterCreate(NovelCharacterBase):
    novel_id: int = Field(..., description="所属小说ID")


class NovelCharacterUpdate(BaseModel):
    name: Optional[str] = Field(None, description="角色名称")
    description: Optional[str] = Field(None, description="角色描述")
    personality: Optional[str] = Field(None, description="性格特征")
    background: Optional[str] = Field(None, description="背景故事")
    abilities: Optional[List[str]] = Field(None, description="能力技能")
    relationships: Optional[Dict[str, Any]] = Field(None, description="人物关系")
    avatar_image: Optional[str] = Field(None, description="角色头像")
    role_type: Optional[RoleTypeEnum] = Field(None, description="角色类型")
    importance_level: Optional[int] = Field(None, description="重要程度(1-10)")
    tags: Optional[List[str]] = Field(None, description="标签")


class NovelCharacter(NovelCharacterBase):
    id: int = Field(..., description="角色ID")
    novel_id: int = Field(..., description="所属小说ID")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")
    is_active: bool = Field(True, description="是否激活")

    class Config:
        from_attributes = True


class Novel(NovelBase):
    id: int = Field(..., description="小说ID")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")
    is_active: bool = Field(True, description="是否激活")

    class Config:
        from_attributes = True


class NovelWithCharacters(Novel):
    characters: List[NovelCharacter] = Field([], description="角色列表")


class CharacterDetailResponse(BaseModel):
    code: int = Field(200, description="响应码")
    message: str = Field("获取成功", description="响应消息")
    data: NovelCharacter = Field(..., description="角色数据")


class NovelListResponse(BaseModel):
    code: int = Field(200, description="响应码")
    message: str = Field("获取成功", description="响应消息")
    data: List[NovelWithCharacters] = Field(..., description="小说列表")


class NovelDetailResponse(BaseModel):
    code: int = Field(200, description="响应码")
    message: str = Field("获取成功", description="响应消息")
    data: NovelWithCharacters = Field(..., description="小说数据") 